﻿using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Hangjing.Common;
using Hangjing.SQLServerDAL;
using Hangjing.Model;
using System.Net;
using org.in2bits.MyXls;
using System.Collections.Generic;
using System; //生成excel时需要使用的引用
using System.IO;
using MVCNPOIHelper.Common;

/// <summary>
/// 分站费用
/// </summary>
public partial class Admin_shop_dUserAddMoneyToExcel : System.Web.UI.Page
{

    Deliver bll = new Deliver();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string SqlWhere = WebUtility.FixgetCookie("webStatisticsExcleOrder");
            if (SqlWhere!= null && SqlWhere != "")
            {
                SqlWhere = Server.UrlDecode(SqlWhere);
            }
            else
            {
                SqlWhere = "1=1";
            }

            GetExcel(SqlWhere);

        }
    }

    protected void GetExcel(string SqlWhere)
    {
        var models = new List<AccountingDetailInfo>();

        IList<AccountsInfo> accountlist = new Accounts().GetsubList();
        IList<DeliverGroupInfo> DeliverGrouplist = SectionProxyData.GetEdelivergroupList();
        IList<SectionInfo> Sectionlist = SectionProxyData.GetSectionList();
        IList<AccountingDetailInfo> datalist = new AccountingDetail().GetList(99999, 1, SqlWhere, "dataid", 1);
        foreach (var item in datalist)
        {
            foreach (var account in accountlist)
            {
                if (item.UserId == account.ID)
                {
                    item.accountname = account.classname;
                    break;
                }

            }
            foreach (var account in accountlist)
            {

                if (item.PayDate == account.ID)
                {
                    item.subaccountname = account.classname;
                    break;
                }

            }
            foreach (var account in Sectionlist)
            {

                if (item.State == account.SectionID)
                {
                    item.accountuser = account.SectionName;
                    break;
                }

            }
            foreach (var group in DeliverGrouplist)
            {

                if (item.PayType == group.ID)
                {
                    item.delivername += "-" + group.classname;
                    break;
                }

            }
        }

        models.AddRange(datalist);

        Stream stream = null;

        string dirurl = Hangjing.WebCommon.WebHelper.CreateDirectoryByMonth(Context);
        string filepath = dirurl + DateTime.Now.ToString("yyyyMMddHHmm")+".xls";

        var excelColumns = ExcelColumns.Create(Server.MapPath("~/App_Data/sysfeeImport.xml"), "root/export/row");
        if (models.Count < 36000)
        {
            stream = NpoiExcelHelper.RenderToExcelIList(models, excelColumns, NpoiExcelHelper.ExcelType.Excel03);
        }

        else
        {
            stream = NpoiExcelHelper.RenderToExcelIList(models, excelColumns, NpoiExcelHelper.ExcelType.Excel07);
           
        }

        StreamWriter sw = new StreamWriter(Server.MapPath(filepath));
        stream.CopyTo(sw.BaseStream);
        sw.Flush();
        sw.Close();

        Response.Redirect(filepath.Replace("~",""));



    }
}
